//
//  UIButton+Extension.swift
//  PMS
//
//  Created by 何海涛 on 16/2/23.
//  Copyright © 2016年 途家. All rights reserved.
//

import Foundation

public typealias MutexColor = (normalBg: UIColor,highlightedBg: UIColor,normal: UIColor, highlighted : UIColor, border: UIColor, borderWidth : CGFloat)
public class MutexColors {
    public static let Confirm : MutexColor = (CommonColors.c_fd8238, CommonColors.c_fd8238, UIColor.white, UIColor.white, CommonColors.c_fd8238, CommonDefine.kPixel)
    public static let AssistConfirm : MutexColor = MutexColors.Confirm//(VS.assistBlue, UIColor.whiteColor(), UIColor.whiteColor(), VS.assistBlue, VS.assistBlue, CommonDefine.kPixel)
    public static let Cancel : MutexColor = (UIColor.white, UIColor.white, CommonColors.c_fd8238, CommonColors.c_fd8238, CommonColors.c_fd8238, CommonDefine.kPixel)
    public static let PopConfirm : MutexColor = (UIColor.clear, UIColor.clear, VS.mainBlue, VS.mainBlue, UIColor.clear, 0)
    public static let PopCancel : MutexColor = (UIColor.clear, UIColor.clear, VS.txtSubhead, VS.txtSubhead, UIColor.clear, 0)
    public static let Red : MutexColor = (UIColor.white,VS.assistRed,VS.assistRed,UIColor.white,VS.assistRed,CommonDefine.kPixel)
    public static let RedReverse : MutexColor = (VS.assistRed,UIColor.white,UIColor.white,VS.assistRed,VS.assistRed,CommonDefine.kPixel)
    public static let Disable : MutexColor = (VS.txtSecondary, VS.txtSecondary,UIColor.white, UIColor.white, VS.txtSecondary, CommonDefine.kPixel)
    public static let DisableReverse : MutexColor = (UIColor.white, UIColor.white,VS.txtSecondary, VS.txtSecondary, VS.txtSecondary, CommonDefine.kPixel)
    public static let Clear : MutexColor = (UIColor.white,UIColor.white,VS.txtMain,VS.txtSubhead,VS.txtSecondary, CommonDefine.kPixel)
    public static let AssistBlue  : MutexColor = (UIColor.clear,UIColor.clear,VS.assistBlue, VS.assistBlue.toNewAlpha(0.5), UIColor.clear,0)
    public static let MainBlue  : MutexColor = (UIColor.clear,UIColor.clear,VS.mainBlue, VS.mainBlue.toNewAlpha(0.5), UIColor.clear,0)
    public static let White  : MutexColor = (UIColor.clear,UIColor.clear,UIColor.white, UIColor.white.toNewAlpha(0.5), UIColor.clear,0)
    public static let Bar : MutexColor = (UIColor.clear,UIColor.clear,UIColor.white,UIColor.white.toNewAlpha(0.5), UIColor.clear,0)
    public static let None : MutexColor = (UIColor.clear, UIColor.clear, VS.txtSecondary, VS.txtSubhead, UIColor.clear, 0)
    public static let NoneSub : MutexColor = (UIColor.clear, UIColor.clear, VS.txtSubhead, VS.txtSecondary, UIColor.clear, 0)
    public static let NoneBlack : MutexColor = (UIColor.clear, UIColor.clear, VS.txtMain, VS.txtSecondary, UIColor.clear, 0)
    public static let NoneOrange : MutexColor = (UIColor.clear, UIColor.clear, VS.bgOrange, VS.bgOrange.toNewAlpha(0.5), UIColor.clear, 0)
    public static let Anniversary: MutexColor = (VS.anniversaryGreen, VS.anniversaryGreen, UIColor.white, UIColor.white, UIColor.clear,0)
}

// copied from http://stackoverflow.com/questions/26600980/
extension UIButton {
    public func setBackgroundColor(_ color: UIColor, forState: UIControl.State) {
        UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
        if let cxt = UIGraphicsGetCurrentContext() {
            cxt.setFillColor(color.cgColor)
            cxt.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
        }
        let colorImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        self.setBackgroundImage(colorImage, for: forState)
    }
}

extension UIButton {
    
    /**
     set color pairs for normal state and highlighted state
     
     - parameter mutexColor: color pairs, see <b>MutexColors</b>
     */
    public func setMutexColor(colorPairs mutexColor : MutexColor) {
        self.setTitleColor(mutexColor.normal, for: UIControl.State())
        self.setTitleColor(mutexColor.highlighted, for: .highlighted)
        
        self.setBackgroundColor(mutexColor.normalBg, forState: UIControl.State())
        self.setBackgroundColor(mutexColor.highlightedBg, forState: .highlighted)
        
        self.setBorder(width: mutexColor.borderWidth,color: mutexColor.border)
    }
    
}

extension UIButton {
    // 设置图片和文字居中，图片在上，文字在下。必须有图片和文字。
    public func setImageAtTopAndTextAtBottom() {
        if let s1 = self.imageView?.image?.size {
            if let s2 = self.titleLabel?.sizeThatFits(CGSize(width: CommonDefine.kScreenWidth, height: CommonDefine.kScreenFullHeight)) {
                self.contentHorizontalAlignment = .center
                self.contentVerticalAlignment = .center
                self.titleLabel?.textAlignment = .center
                
                self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: s2.height, right: -s2.width)
                self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -s1.width, bottom: -s1.height, right: 0)
            }
        }
    }
}


extension UIButton {
    
    private struct AssociatedKeys {
        static var actionKey: String = "ActionAssociatedKey"
    }
    
    private var handler: (() -> Void)? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.actionKey) as? () -> Void
        } set (value) {
            objc_setAssociatedObject(self, &AssociatedKeys.actionKey, value, .OBJC_ASSOCIATION_COPY)
        }
    }
    
    
    public convenience init(_ action: (() -> Void)?, for event: UIControl.Event = .touchUpInside) {
        self.init(type: .custom)
        self.handler = action
        self.addTarget(self, action: #selector(doHandler(_:)), for: event)
    }
    
    @objc func doHandler(_ sender: UIButton) {
        handler?()
    }
}

